package com.object.oriented.exam.system5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.object.oriented.exam.system1.dao.ProblemMapping;
import com.object.oriented.exam.system1.entity.Problem;
import com.object.oriented.exam.system2.dao.ProblemPropertyMapping;
import com.object.oriented.exam.system2.entity.ProblemProperty;
import com.object.oriented.exam.system5.entity.Answer;
import com.object.oriented.exam.system5.entity.Score;
import com.object.oriented.exam.system5.mapper.AnswerMapper;
import com.object.oriented.exam.system5.mapper.ScoreMapper;
import com.object.oriented.exam.system5.service.AnswerService;
import com.object.oriented.exam.system5.service.ScoreService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xdf
 * @date 2023-06-14
 */
@Service
public class ScoreServiceImpl implements ScoreService {
    @Resource
    ScoreMapper scoreMapper;

    @Resource
    AnswerMapper answerMapper;

    @Resource
    ProblemMapping problemMapping;

    @Resource
    ProblemPropertyMapping problemPropertyMapping;

    @Resource
    AnswerService answerService;

    @Override
    public Score getScore(Long studentId, Long examId) {
        return getScores(studentId,examId,1L,1L).getRecords().get(0);
    }

    @Override
    public Page<Score> getScores(Long studentId, Long examId,Long current, Long size) {
        QueryWrapper<Score> scoreQueryWrapper=new QueryWrapper<>();
        scoreQueryWrapper
                .eq(!ObjectUtils.isEmpty(studentId),"student_id",studentId)
                .eq("exam_id",examId)
        ;
        Page<Score> resultPage=new Page<>(current,size);
        scoreMapper.selectPage(resultPage,scoreQueryWrapper);
        return resultPage;
    }

    @Override
    public Page<Score> getScoresInExam(Long examId, Long current, Long size) {
        return getScores(null,examId,current,size);
    }

    @Override
    public Boolean finish(Long studentId, Long examId,List<Answer> answerList) {
        for(Answer answer:answerList){
            answer.setExamId(examId);
            answer.setStudentId(studentId);
            answerService.commitAnswer(answer);
        }
        QueryWrapper<Score> scoreQueryWrapper=new QueryWrapper<>();
        scoreQueryWrapper
                .eq("student_id",studentId)
                .eq("exam_id",examId)
                ;
        Score score=scoreMapper.selectOne(scoreQueryWrapper);
        if(score.getStatus()==1){
            return false;
        }
        score.setStatus((byte) 1);

        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq("exam_id",examId)
                .eq("student_id",studentId)
                ;
//        List<Answer> answers=answerMapper.selectList(answerQueryWrapper);
        // 评分
        for (Answer answer:answerList){
            Problem problem=problemMapping.selectById(answer.getQuestionId());
            if(problem.getType()==1||problem.getType()==2){
                // 客观题，即选择和填空，自动评分
                QueryWrapper<ProblemProperty> problemPropertyQueryWrapper=new QueryWrapper<>();
                problemPropertyQueryWrapper
                        .eq("question_id",answer.getQuestionId())
                        ;
                ProblemProperty problemProperty=problemPropertyMapping.selectOne(problemPropertyQueryWrapper);
                if(answer.getAnswer().equals(problemProperty.getAnswer())){
                    // 答案正确
                    answer.setScore(problem.getDefaultScore());
                    answer.setResult((byte) 0);
                }
                else {
                    // 答案不正确
                    answer.setScore(0);
                    answer.setResult((byte) 1);
                }
            }
            else {
                // 待评分
                answer.setResult((byte) -1);
            }
            answerMapper.updateById(answer);
        }
        return scoreMapper.updateById(score)>0;
    }

    @Override
    public Integer compute(Long studentId, Long examId){
        QueryWrapper<Score> scoreQueryWrapper=new QueryWrapper<>();
        scoreQueryWrapper
                .eq("student_id",studentId)
                .eq("exam_id",examId);
        Score score=scoreMapper.selectOne(scoreQueryWrapper);
        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq("student_id",studentId)
                .eq("exam_id",examId);
        List<Answer> answers=answerMapper.selectList(answerQueryWrapper);
        int total=0;
        for(Answer answer:answers){
            total=total+answer.getScore();
        }
        score.setTotalScore(total);
        scoreMapper.updateById(score);
        return total;
    }
}
